package dhm.com.hpx.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.CuponAdapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.CuponBean;
import dhm.com.hpx.utils.SpUtils;

public class CouponActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.text_nouse)
    TextView textNouse;
    @BindView(R.id.line_nouse)
    TextView lineNouse;
    @BindView(R.id.text_used)
    TextView textUsed;
    @BindView(R.id.line_used)
    TextView lineUsed;
    @BindView(R.id.text_noused)
    TextView textNoused;
    @BindView(R.id.line_noused)
    TextView lineNoused;
    @BindView(R.id.recy)
    XRecyclerView recy;
    private String uid;

    private PressenterImpl pressenter;
    private CuponAdapter adapter;
    private List<CuponBean.DataBean> beanList;
    private String type;

    @Override
    protected int getLayout() {
        return R.layout.activity_coupon;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        type = getIntent().getStringExtra("type");

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
        adapter = new CuponAdapter(this);
        adapter.setdeleteClick(new CuponAdapter.OnClick() {
            @Override
            public void item(int posttion) {
                if (type.equals("1")){
                    Intent intent=new Intent(CouponActivity.this,MemberActivity.class);
                    intent.putExtra("receive_id",beanList.get(posttion).getReceive_id()+"");
                    intent.putExtra("title",beanList.get(posttion).getTitle()+"");
                    intent.putExtra("with_amount",beanList.get(posttion).getWith_amount()+"");
                    intent.putExtra("amount",beanList.get(posttion).getAmount()+"");
                    startActivity(intent);
                }else {
                    Intent intent = getIntent();
                    intent.putExtra("receive_id",beanList.get(posttion).getReceive_id()+"");
                    intent.putExtra("title",beanList.get(posttion).getTitle()+"");
                    intent.putExtra("with_amount",beanList.get(posttion).getWith_amount()+"");
                    intent.putExtra("amount",beanList.get(posttion).getAmount()+"");
                    setResult(100,intent);
                    finish();
                }
            }
        });
        recy.setAdapter(adapter);

        recy.setPullRefreshEnabled(true);
        recy.setLoadingMoreEnabled(true);
        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                getdata();
            }

            @Override
            public void onLoadMore() {
                getdata();
            }
        });

        uid = SpUtils.getString(this, "uid");
        page=1;
    }

    @Override
    protected void initData() {
        getdata();
    }

    private void getdata() {
        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("status",cuponstatus+"");
        map.put("page",page+"");
        pressenter.sendMessage(CouponActivity.this,Constant.coupon_list,map,CuponBean.class);
    }


    int page=1;
    int cuponstatus=0;

    @SuppressLint("NewApi")
    @OnClick({R.id.back, R.id.nouse, R.id.used, R.id.noused})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.nouse:
                cuponstatus=0;
                page=1;
                textNouse.setTextColor(getResources().getColor(R.color.main));
                textUsed.setTextColor(Color.parseColor("#999999"));
                textNoused.setTextColor(Color.parseColor("#999999"));
                lineNouse.setVisibility(View.VISIBLE);
                lineNoused.setVisibility(View.INVISIBLE);
                lineUsed.setVisibility(View.INVISIBLE);
                getdata();
                break;
            case R.id.used:
                cuponstatus=1;
                page=1;
                textUsed.setTextColor(getResources().getColor(R.color.main));
                textNouse.setTextColor(Color.parseColor("#999999"));
                textNoused.setTextColor(Color.parseColor("#999999"));
                lineUsed.setVisibility(View.VISIBLE);
                lineNoused.setVisibility(View.INVISIBLE);
                lineNouse.setVisibility(View.INVISIBLE);
                getdata();
                break;
            case R.id.noused:
                cuponstatus=2;
                page=1;
                textNoused.setTextColor(getResources().getColor(R.color.main));
                textUsed.setTextColor(Color.parseColor("#999999"));
                textNouse.setTextColor(Color.parseColor("#999999"));
                lineNoused.setVisibility(View.VISIBLE);
                lineNouse.setVisibility(View.INVISIBLE);
                lineUsed.setVisibility(View.INVISIBLE);
                getdata();
                break;
                default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof CuponBean){
            CuponBean cuponBean= (CuponBean) data;

            if (cuponBean.getCode()==1){
                if (page==1){
                    beanList = cuponBean.getData();
                }else {
                    beanList.addAll(cuponBean.getData());
                }
                adapter.setList(beanList);
                page++;
            }
            recy.loadMoreComplete();
            recy.refreshComplete();

        }
    }

    @Override
    public void fail(String error) {

    }
}
